package com.mygame.common;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mygame.common.codeGenerator.FreemarkerUtil;
import com.mygame.common.codeGenerator.TableParseUtil;
import com.mygame.common.codeGenerator.entity.ClassInfo;
import com.mygame.common.codeGenerator.entity.ParamInfo;
import com.mygame.common.component.utils.StringUtils;
import com.mygame.common.utils.FileUtil;
import com.mygame.common.utils.JsonUtils;
import com.mygame.common.utils.PathUtil;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class GeneratorDBModel {
    public static void main(String[] args) throws Exception {
        String baseDir = PathUtil.getConfigBasePath("code_generator");
        JSONObject config = JsonUtils.read(baseDir, "config.json");

        String fileName = config.getString("sql_file_name");
        String tableSql = FileUtil.readFile(baseDir , fileName);
        String javaOutPath = config.getString("java_out_path");

        String[] tableArr = tableSql.split(";");
        for(String str : tableArr) {
            if(StringUtils.isBlank(str)){
                continue;
            }
            ParamInfo paramInfo = new ParamInfo();
            paramInfo.setOptions(new HashMap<>());
            paramInfo.setTableSql(str);

            JSONObject param = config.getJSONObject("templates_param");
            paramInfo.getOptions().put("dataType", param.getString("dataType"));
            paramInfo.getOptions().put("authorName", param.getString("authorName"));
            paramInfo.getOptions().put("packageName", param.getString("packageName"));
            paramInfo.getOptions().put("returnUtilSuccess", param.getString("returnUtilSuccess"));
            paramInfo.getOptions().put("returnUtilFailure", param.getString("returnUtilFailure"));
            paramInfo.getOptions().put("isPackageType", param.getBoolean("isPackageType"));
            paramInfo.getOptions().put("isSwagger", param.getBoolean("isSwagger"));
            paramInfo.getOptions().put("isAutoImport", param.getBoolean("isAutoImport"));
            paramInfo.getOptions().put("isWithPackage", param.getBoolean("isWithPackage"));
            paramInfo.getOptions().put("isComment", param.getBoolean("isComment"));
            paramInfo.getOptions().put("isLombok", param.getBoolean("isLombok"));
            paramInfo.getOptions().put("ignorePrefix", param.getString("ignorePrefix"));
            paramInfo.getOptions().put("tinyintTransType", param.getString("tinyintTransType"));
            paramInfo.getOptions().put("nameCaseType", param.getString("nameCaseType"));
            paramInfo.getOptions().put("timeTransType", param.getString("timeTransType"));
//        paramInfo.getOptions().put("tableName", param.getString("tableName"));

            ClassInfo classInfo = TableParseUtil.processTableIntoClassInfo(paramInfo);
            classInfo.setClassName(classInfo.getClassName() + "Model");
            paramInfo.getOptions().put("classInfo", classInfo);

            JSONArray parentTemplates = config.getJSONArray("templates");

            Map<String, Object> params = paramInfo.getOptions();

            for (int i = 0; i < parentTemplates.size(); i++) {
                JSONObject parentTemplateObj = parentTemplates.getJSONObject(i);
                String name = parentTemplateObj.getString("name");
                String result = FreemarkerUtil.processString(name + ".ftl", params);
                String className = classInfo.getClassName();
                File file = new File(javaOutPath, className + ".java");
                FileUtil.writeFile(file, result);
            }
        }
//        System.out.println(result.get("model"));



    }
}
